﻿using System.Globalization;

using ApocDev.TownsApi.Aspects;

namespace ApocDev.TownsApi
{
    [NotifyPropertyChanged]
    public class RandomRoll
    {
        public RandomRoll(string input)
        {
            // First try and split based on all the split chars!
            var fullRandomSplit = input.Split('d', '+');
            // If we have 3 things here, then we can just parse it in quickly.
            if (fullRandomSplit.Length == 3)
            {
                Minimum = int.Parse(fullRandomSplit[0]);
                Maximum = int.Parse(fullRandomSplit[1]);
                Additive = int.Parse(fullRandomSplit[2]);
            }
            else if (fullRandomSplit.Length == 2)
            {
                // Pure dice roll.
                Minimum = int.Parse(fullRandomSplit[0]);
                Maximum = int.Parse(fullRandomSplit[1]);
            }
            else
            {
                // Flat number. :)
                Additive = int.Parse(input);
            }
        }

        public int Minimum { get; set; }
        public int Maximum { get; set; }
        public int Additive { get; set; }

        public bool IsZero { get { return Minimum == 0 && Maximum == 0 && Additive == 0; } }

        public string StringValue
        {
            get
            {
                // 1d2880
                if (Additive == 0)
                    return Minimum + "d" + Maximum;

                // 1
                if (Minimum == 0 && Maximum == 0)
                    return Additive.ToString(CultureInfo.InvariantCulture);

                // 1d2880+2880
                return Minimum + "d" + Maximum + "+" + Additive;
            }
        }

        public override string ToString()
        {
            return StringValue;
        }
    }
}
